package org.androidpn.server.util;

import org.androidpn.server.entity.FriendMessage.FriendMessageStatus;
import org.androidpn.server.entity.FriendMessage.FriendMessageType;
import org.androidpn.server.entity.Role.RoleStatus;
import org.androidpn.server.entity.Room.RoomStatus;
import org.androidpn.server.entity.RoomMessage.RoomMessageType;


/**
 * xml工具类
 * 
 * @author 蔡琪
 * 
 */
public class XMLUtils {

	public static FriendMessageType getFriendMessageTypFromStr(
			String friendMessageType) {
		try {
			return FriendMessageType.valueOf(friendMessageType);
		} catch (Exception e) {
			return FriendMessageType.values()[0];
		}
	}

	public static FriendMessageStatus getFriendMessageStatus(
			String friendMessageStatus) {
		try {
			return FriendMessageStatus.valueOf(friendMessageStatus);
		} catch (Exception e) {
			return FriendMessageStatus.values()[0];
		}
	}

	/**
	 * 从str中获取房间消息类型
	 * 
	 * @param str
	 * @return
	 */
	public static RoomMessageType getRoomMessageTypeFromStr(String str) {
		try {
			return RoomMessageType.valueOf(str);
		} catch (Exception e) {
			return RoomMessageType.values()[0];
		}
	}

	/**
	 * 从str中获取房间状态信息
	 * 
	 * @param str
	 * @return
	 */
	public static RoomStatus getRoomStatusFromStr(String str) {
		try {
			return RoomStatus.valueOf(str);
		} catch (Exception e) {
			return RoomStatus.values()[0];
		}
	}

	/**
	 * 从str中获取角色状态RoleStatus
	 * 
	 * @param str
	 * @return
	 */
	public static RoleStatus getRoleStatusFromStr(String str) {
		try {
			return RoleStatus.valueOf(str);
		} catch (Exception e) {
			return RoleStatus.values()[0];
		}
	}

	/**
	 * 从str中获取boolean
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean getBooleanFromStr(String str) {
		try {
			return Boolean.valueOf(str);
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 从str中获取Long
	 * 
	 * @param str
	 * @return
	 */
	public static Long getLongFromStr(String str) {
		try {
			return Long.valueOf(str);
		} catch (Exception e) {
			return new Long(0);
		}
	}

	/**
	 * 创建单个节点
	 * 
	 * @param nodename
	 *            节点名
	 * @param value
	 *            节点值
	 * @return 节点xml字符串
	 */
	public static String createSingleNode(String nodename, String value) {
		if (nodename == null || nodename.equals("") || value == null
				|| value.equals("")) {
			return "";
		}
		String strValue = "";
		if (value != null) {
			strValue = value.toString();
		}
		String node = "<" + nodename + ">" + strValue + "</" + nodename + ">";
		return node;
	}

	/**
	 * 创建单个节点
	 * 
	 * @param nodename
	 *            节点名
	 * @param value
	 *            节点值
	 * @return 节点xml字符串
	 */
	public static String createSingleNode(String nodename, Long value) {
		if (nodename == null || nodename.equals("") || value == null
				|| value.equals("")) {
			return "";
		}
		String strValue = "";
		if (value != null) {
			strValue = value.toString();
		}
		String node = "<" + nodename + ">" + strValue + "</" + nodename + ">";
		return node;
	}

	/**
	 * 创建单个节点
	 * 
	 * @param nodename
	 *            节点名
	 * @param value
	 *            节点值
	 * @return 节点xml字符串
	 */
	public static String createSingleNode(String nodename, Boolean value) {
		if (nodename == null || nodename.equals("") || value == null
				|| value.equals("")) {
			return "";
		}
		String strValue = "";
		if (value != null) {
			strValue = value.toString();
		}
		String node = "<" + nodename + ">" + strValue + "</" + nodename + ">";
		return node;
	}

	/**
	 * 创建单个节点
	 * 
	 * @param nodename
	 *            节点名
	 * @param value
	 *            节点值
	 * @return 节点xml字符串
	 */
	public static String createSingleNode(String nodename, RoleStatus value) {
		if (nodename == null || nodename.equals("")) {
			return "";
		}
		String strValue = "";
		if (value != null) {
			strValue = value.toString();
		}
		String node = "<" + nodename + ">" + strValue + "</" + nodename + ">";
		return node;
	}

	/**
	 * 创建单个节点
	 * 
	 * @param nodename
	 *            节点名
	 * @param value
	 *            节点值
	 * @return 节点xml字符串
	 */
	public static String createSingleNode(String nodename, RoomStatus value) {
		if (nodename == null || nodename.equals("")) {
			return "";
		}
		String strValue = "";
		if (value != null) {
			strValue = value.toString();
		}
		String node = "<" + nodename + ">" + strValue + "</" + nodename + ">";
		return node;
	}

	/**
	 * 创建单个节点
	 * 
	 * @param nodename
	 *            节点名
	 * @param value
	 *            节点值
	 * @return 节点xml字符串
	 */
	public static String createSingleNode(String nodename, RoomMessageType type) {
		if (nodename == null || nodename.equals("")) {
			return "";
		}
		String strValue = "";
		if (type != null) {
			strValue = type.toString();
		}
		String node = "<" + nodename + ">" + strValue + "</" + nodename + ">";
		return node;
	}

	public static Object createSingleNode(String nodename,
			FriendMessageType friendMessageType) {
		if (nodename == null || nodename.equals("")) {
			return "";
		}
		String strValue = "";
		if (friendMessageType != null) {
			strValue = friendMessageType.toString();
		}
		String node = "<" + nodename + ">" + strValue + "</" + nodename + ">";
		return node;
	}

	public static Object createSingleNode(String nodename,
			FriendMessageStatus friendMessageStatus) {
		if (nodename == null || nodename.equals("")) {
			return "";
		}
		String strValue = "";
		if (friendMessageStatus != null) {
			strValue = friendMessageStatus.toString();
		}
		String node = "<" + nodename + ">" + strValue + "</" + nodename + ">";
		return node;
	}

}
